import HamsterRunner from "../views/hamster/worker/hamster_runner";
import HamsterTgweb from "../views/hamster/worker/hamster_tgweb";
import PiggyTgweb from "@/views/piggy/worker/piggy_tgweb";
import PiggyRunner from "@/views/piggy/worker/piggy_runner";
import BlumRunner from "@/views/blum/worker/blum_runner";
import BlumTgweb from "@/views/blum/worker/blum_tgweb";
import TonstationRunner from "@/views/tonstation/worker/tonstation_runner";
import TonstationTgweb from "@/views/tonstation/worker/tonstation_tgweb";
import DuckchainRunner from "@/views/duckchain/worker/duckchain_runner";
import DuckchainTgweb from "@/views/duckchain/worker/duckchain_tgweb";
import TimefarmRunner from "@/views/timefarm/worker/timefarm_runner";
import TimefarmTgweb from "@/views/timefarm/worker/timefarm_tgweb";
import TabizooTgweb from "@/views/tabizoo/worker/tabizoo_tgweb";
import TabizooRunner from "@/views/tabizoo/worker/tabizoo_runner";
import GramTgweb from "@/views/cats/worker/cats_tgweb";
import CatsRunner from "@/views/cats/worker/cats_runner";
import MemefiRunner from "@/views/memefi/worker/memefi_runner";
import MemefiTgweb from "@/views/memefi/worker/memefi_tgweb";
import CowsRunner from "@/views/cows/worker/cows_runner";
import CowsTgweb from "@/views/cows/worker/cows_tgweb";
import MajorRunner from "@/views/major/worker/major_runner";
import TomarketRunner from "@/views/tomarket/worker/tomarket_runner";
import SidekickRunner from "@/views/sidekick/worker/sidekick_runner";


class WorkerManager {
  constructor(parameters) {
    this.runnerMap = {
      hamster: new HamsterRunner(),
      piggy: new PiggyRunner(),
      blum: new BlumRunner(),
      tonstation: new TonstationRunner(),
      duckchain: new DuckchainRunner(),
      timefarm: new TimefarmRunner(),
      tabizoo: new TabizooRunner(),
      cats: new CatsRunner(),
      major: new MajorRunner(),
      memefi: new MemefiRunner(),
      cows: new CowsRunner(),
      tomarket: new TomarketRunner(),
      sidekick: new SidekickRunner(),
    }
    this.tgwebMap = {
      hamster: new HamsterTgweb(),
      piggy: new PiggyTgweb(),
      blum: new BlumTgweb(),
      tonstation: new TonstationTgweb(),
      duckchain: new DuckchainTgweb(),
      timefarm: new TimefarmTgweb(),
      tabizoo: new TabizooTgweb(),
      cats: new GramTgweb("cats", "ios"),
      major: new GramTgweb("major", "ios"),
      memefi: new MemefiTgweb("memefi", "ios"),
      cows: new CowsTgweb("cows", "ios"),
      tomarket: new GramTgweb("tomarket", "ios"),
      sidekick: new GramTgweb("sidekick", "ios"),
    }
  }

  isAllGameRunning() {
    return this.isGameRunning;
  }


  /**
   * 其他游戏类似写法
   * 获取tgweb后就启动游戏业务循环
   * 如果tgweb过期，则自定义timer时间间隔重复获取
   */
  async launchAllGame() {
    if (this.isGameRunning) {
      return
    }
    this.isGameRunning = true
    console.log("all games start")
    // this.test = new SidekickRunner()
    // await this.test.start()
    // return
    const gameKeys = [
      // "hamster",
      // "cats",

      "tomarket",
      "cows",
      "major",
      "piggy",
      "blum",
      "duckchain",
      "timefarm",
      "tabizoo",
      "sidekick",
    ]
    for (let index = 0; index < gameKeys.length; index++) {
      const key = gameKeys[index];
      const tgweb = this.tgwebMap[key]
      await tgweb.allRun().then(res => {
        const runner = this.runnerMap[key]
        runner.start()
      })
    }

    const tonwebWorksNeedsLoop = [
      "tonstation",
      "memefi",
    ]

    while(true) {
      for (let index = 0; index < tonwebWorksNeedsLoop.length; index++) {
        const key = tonwebWorksNeedsLoop[index];
        const tgweb = this.tgwebMap[key]
        await tgweb.allRun();
      }
    }
  }
}

const worker = new WorkerManager()

export default worker